package com.wisezone.workflow.dao.hibernate;

import java.util.List;

import com.wisezone.common.dao.impl.BaseHibernateDaoImpl;
import com.wisezone.workflow.bean.WfActivityInstUser;
import com.wisezone.workflow.bean.WfActivityInstance;
import com.wisezone.workflow.bean.form.WfActivityInstUserForm;
import com.wisezone.workflow.bean.search.WfActivityInstUserSearch;
import com.wisezone.workflow.dao.WfActivityInstUserDao;
import com.wisezone.workflow.exception.WfDaoException;

/**
 * hiberante 活动用户实现类
 * @author tss 2009-09-05
 * 
 */
public class WfActivityInstUserDaoImpl extends BaseHibernateDaoImpl implements WfActivityInstUserDao {

    public WfActivityInstUserDaoImpl() {
        super();
    }
    /**
     * 删除未参与活动的记录
     * @param ai
     * @throws WfDaoException
     */
    public void deleteOhter(WfActivityInstance ai) throws WfDaoException {
    	
    	//删除用户
    	StringBuffer sb = new StringBuffer("delete from WfActivityInstUser t ");
        sb.append("where t.userId !=: userId and processInstId =: processInstId and activityInstId =: activityInstId ");
        sb.append("and t.userId not in ( select t2.actor from WfActivityInstance t2 ");
        sb.append(" where t2.activityId =: activityId ) ");
    	String hql = sb.toString();
        this.execute(hql,ai);
        
        //删除用户组
        sb = new StringBuffer("delete from WfActivityInstUser t ");
        sb.append("where t.processInstId =: processInstId and t.activityInstId =: activityInstId and t.userGroupId is not null ");
        hql = sb.toString();
    	this.execute(hql,ai);
    }
    
    /**
     * 根据流程实例Id 和 用户ID 逻辑删除
     * @param processInstId
     * @param userId
     * @throws WfDaoException
     */
    public void logicDel(Long processInstId,Long userId) throws WfDaoException{
    	
    	WfActivityInstUserSearch searchEntity = new WfActivityInstUserSearch();
    	searchEntity.setProcessInstId(processInstId);
    	searchEntity.setUserId(userId);
    	StringBuffer sb = new StringBuffer("update WfActivitInstUser t set ");
    	sb.append("t.stateDel = 0 where t.processInstId = ? and t.userId = ? ");
    	String hql = sb.toString();
    	this.execute(hql, new Object[]{processInstId,userId});
    }

    
    
    /**
     * 获得活动和人员列表
     * @param activityInstId
     * @return
     * @throws WfDaoException
     */
    public List<WfActivityInstUser> listByActivityInstId(Long activityInstId) throws WfDaoException{
    	
    	WfActivityInstUserSearch searchEntity = new WfActivityInstUserSearch();
    	searchEntity.setActivityInstId(activityInstId);
    	return this.listAll(searchEntity);
    	
    }
    
    /**
     * 获得活动和人员表单列表
     * @param activityInstId
     * @return
     * @throws WfDaoException
     */
    public List<WfActivityInstUserForm> listFormByActivityInstId(Long activityInstId) throws WfDaoException{
    	
    	WfActivityInstUserSearch searchEntity = new WfActivityInstUserSearch();
    	searchEntity.setActivityInstId(activityInstId);
    	return this.listFormAll(searchEntity);
    	
    }
    

   
   

   
}